﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SequentialPattern;
using SequentialPattern.BasicClass;
using SequentialPattern.AprioriAll;

namespace SPTest
{
    public partial class Form1 : Form
    {
        SequentialPattern.SequentialPattern algorithm = null;

        public Form1()
        {
            InitializeComponent();
            cmbxAlgorithm.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string algorithmMethod = cmbxAlgorithm.Text;
            switch (algorithmMethod)
            {
                case "AprioriAll":
                    algorithm = SequentialPatternFactory.GetAlgorithm(SequentialPatternMethod.AprioriAll);
                    AprioriAll tmpAlgorithm = (AprioriAll)algorithm;
                    tmpAlgorithm.EnableSortPhase = chbxSort.Checked;
                    tmpAlgorithm.EnableMaximalPhase = chbxMaximal.Checked;
                    algorithm = tmpAlgorithm;
                    break;
            }

            algorithm.LoadFile(txtData.Text);
            algorithm.MinSupport = Convert.ToDouble(txtSupport.Text);
            
            StringBuilder sb = new StringBuilder();
            algorithm.Run();
            btnSave.Enabled = true;

            foreach (Pattern pattern in algorithm.SequentialPatterns)
            {
                sb.AppendLine(String.Format("{0}\t{1}", pattern.ToString(), pattern.Support));
            }

            sb.AppendLine("Total Patterns : " + algorithm.SequentialPatterns.Count);
            textBox1.Text = sb.ToString();
            
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.Focus();
            textBox1.ScrollToCaret();
            /*
            SequentialPatternCollection sequentialPatterns;

            // 文字檔測試
            SequentialPatternCollection.SaveTo("pattern_text.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Text);
            // sequentialPatterns = SequentialPatternCollection.ReadFrom("pattern_text.txt", SequentialPatternSaveFormat.Text);
            // SequentialPatternCollection.SaveTo("pattern_text2.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Text);

            // Xml檔測試
            SequentialPatternCollection.SaveTo("pattern_xml.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Xml);
            sequentialPatterns = SequentialPatternCollection.ReadFrom("pattern_xml.txt", SequentialPatternSaveFormat.Xml);
            SequentialPatternCollection.SaveTo("pattern_xml2.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Xml);

            // Binary檔測試
            SequentialPatternCollection.SaveTo("pattern_binary.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Binary);
            sequentialPatterns = SequentialPatternCollection.ReadFrom("pattern_binary.txt", SequentialPatternSaveFormat.Binary);
            SequentialPatternCollection.SaveTo("pattern_binary2.txt", myApriori.SequentialPatterns, SequentialPatternSaveFormat.Binary);
            */
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = saveFileDialog1.FileName;
                switch (Path.GetExtension(filename).ToUpper())
                {
                    case ".TXT":
                        SaveTo(filename, SequentialPatternSaveFormat.Text);
                        break;
                    case ".BIN":
                        SaveTo(filename, SequentialPatternSaveFormat.Binary);
                        break;
                    case ".XML":
                        SaveTo(filename, SequentialPatternSaveFormat.Xml);
                        break;
                    default:
                        throw new Exception("不正確的副檔名");
                }
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog1.FileName;
                switch (Path.GetExtension(openFileDialog1.FileName).ToUpper())
                {
                    case ".TXT":
                        LoadFrom(filename, SequentialPatternSaveFormat.Text);
                        break;
                    case ".BIN":
                        LoadFrom(filename, SequentialPatternSaveFormat.Binary);
                        break;
                    case ".XML":
                        LoadFrom(filename, SequentialPatternSaveFormat.Xml);
                        break;
                    default:
                        throw new Exception("不正確的副檔名");
                }
            }
        }

        public void SaveTo(string filepath, SequentialPatternSaveFormat format)
        {
            SequentialPatternCollection.SaveTo(filepath, this.algorithm.SequentialPatterns, format);
        }

        public void LoadFrom(string filepath, SequentialPatternSaveFormat format)
        {
            SequentialPatternCollection patterns = SequentialPatternCollection.ReadFrom(filepath, format);
            StringBuilder sb = new StringBuilder();
            foreach (Pattern pat in patterns)
            {
                sb.AppendLine(pat.ToString());
            }
            textBox1.Text = sb.ToString();
        }
    }
}
